package com.fs.iot.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fs.common.utils.TreeUtils;
import com.fs.iot.bean.dto.AlarmCategoryDTO;
import com.fs.iot.bean.query.AlarmCategoryQuery;
import com.fs.iot.service.AlarmCategoryEntityService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 报警分类控制器 
 * @author Coder Generator 2025-03-24 17:00:27 
 **/
@RestController
@RequestMapping("/manager/alarm-category")
@Slf4j
@Tag(name = "报警分类控制器")
public class AlarmCategoryController {
    @Autowired
    private AlarmCategoryEntityService service;

    @PostMapping("add")
    @Operation(summary = "新增")
    public void add(@RequestBody @Validated AlarmCategoryDTO req) {
        service.insert(req);
    }

    @PutMapping("update")
    @Operation(summary = "更新")
    public void update(@RequestBody @Validated AlarmCategoryDTO req) {
        service.update(req);
    }

    @PostMapping("save")
    @Operation(summary = "保存")
    public AlarmCategoryDTO save(@RequestBody @Validated AlarmCategoryDTO req) {
        if (StringUtils.isBlank(req.getId())) {
            return service.insert(req);
        }
        service.update(req);
        return req;
    }

    @DeleteMapping("delete/{id}")
    @Operation(summary = "删除")
    public void delete(@PathVariable("id") String id) {
        service.delete(id);
    }

    @GetMapping("detail/{id}")
    @Operation(summary = "根据id查找记录")
    public AlarmCategoryDTO findById(@PathVariable("id") String id) {
        return service.findById(id).orElse(null);
    }

    @PostMapping("page-query")
    @Operation(summary = "查询-分页")
    public IPage<AlarmCategoryDTO> pageQuery(@RequestBody AlarmCategoryQuery query) {
        return service.pageQuery(query);
    }

    @PostMapping("query")
    @Operation(summary = "查询-不分页")
    public List<AlarmCategoryDTO> query(@RequestBody AlarmCategoryQuery query) {
        return service.query(query);
    }

    @GetMapping("tree")
    public List<AlarmCategoryDTO> tree() {
        return TreeUtils.toTree(service.findAll());
    }
}